package com.kty.sdk.okhttp;

import com.kty.sdk.okhttp.https.HttpsUtils;

import java.util.concurrent.TimeUnit;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;

/**
 * TODO:请求的发送 请求参数的配置 https支持
 * Created by 倪彬彬 on 2017/11/27.
 */

public class CommonOkHttpClient {
    private static final int TIME_OUT = 30;//超时参数
    private static OkHttpClient mOkHttpClient;

    static {
        //创建client 对象的构建者
        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();

        //设置超时时间
        okHttpClient.connectTimeout(TIME_OUT, TimeUnit.SECONDS);
        okHttpClient.readTimeout(TIME_OUT, TimeUnit.SECONDS);
        okHttpClient.writeTimeout(TIME_OUT, TimeUnit.SECONDS);

        //允许重定向
        okHttpClient.followRedirects(true);

        //https支持
        okHttpClient.hostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String s, SSLSession sslSession) {
                return true;
            }
        });


        okHttpClient.sslSocketFactory(HttpsUtils.initSSLSocketFactory(), HttpsUtils.initTrustManager());

        //生成client对象
        mOkHttpClient = okHttpClient.build();

    }

    /**
     * 发送具体的http/https请求
     * @param request
     * @param callback
     * @return call
     */
    public static Call sendRequest(Request request , Callback callback){

        Call call=mOkHttpClient.newCall(request);

        call.enqueue(callback);

        return call;

    }
}
